/**
 * Copyright 2011 Christian Padovano
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package it.sijom;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;

/**
 * Handle a cache system for the methods and attributes to improve performances
 * @author Christian Padovano
 * @version 1.0
 * @email christian.padovano@gmail.com
 */
public abstract class AJavaObjectMapperCache extends AJavaObjectMapperConverter {
	
	
	//static class variable for the 'target' annotations
	private final static Map<String,Field[]> annotationCache = new HashMap<String,Field[]>();
	
	private final static Map<String,List<Method>> methodsCache = new HashMap<String,List<Method>>();
	
	
	
	 /**
	  * Create a cache for the 'Target' annotation
	  */
	 protected  Field[] getTargetAnnotations(Object source) {
		 
		 //check in the cache if there are the attributes required for the specified source object
		  if (annotationCache.containsKey(source.getClass().getName())) {
			  return annotationCache.get(source.getClass().getName());
		  }
		  //call the parent object method..
		  Field[] _fields =super.getTargetAnnotations(source);
		  //store the array of field in the cache..
		  annotationCache.put(source.getClass().getName(), _fields);
		  return _fields; 
	 }
	 
	protected List<Method> getMethods(Class targetClass,Class annotation) throws SecurityException, ClassNotFoundException {
		 String key = targetClass.getName()+annotation.getName();
		
		   if (methodsCache.containsKey(key)) {
			   return methodsCache.get(key);
		   }
		 
		 List<Method> _dummy = super.getMethods(targetClass,annotation);
		 methodsCache.put(key, _dummy);
		 return _dummy;
	 }

}
